<template>
  <div id="VIP_index" class="all">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>库存管理 </el-breadcrumb-item>
      <el-breadcrumb-item>出库订单</el-breadcrumb-item>
    </el-breadcrumb>

    <br>


    <div class="table-container">
      <!-- <el-descriptions title="" class="custom-descriptions"></el-descriptions> -->
      <span style="margin-right: 91%;padding: 10px;"><b>出库查询</b></span>
      <el-divider class="custom-divider"></el-divider>
      <el-form :model="selform" ref="form">
        <el-row>
          <el-col :span="6">
            <el-input v-model="selform.purchaseId" style="width: 95%;padding: 10px;" placeholder="购买订单编号" />
          </el-col>
          <el-col :span="6">
            <el-input v-model="selform.userName" style="width: 95%;padding: 10px;" placeholder="用户名" />
          </el-col>
          <el-col :span="6">
            <el-date-picker v-model="selform.orderTime" type="datetimerange" :picker-options="pickerOptions"
              range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 95%;margin:10px;"
              align="right"></el-date-picker>
          </el-col>
          <el-col :span="4" style="text-align: center;padding: 8px 20px;">
            <el-button type="primary" style="font-size: 16px;" @click="submitselMessage" icon="el-icon-search"> 搜索</el-button>



            <el-dialog title="订单信息详情" :visible.sync="showMessage" @close="closeShowMessage" :close-on-click-modal="false">
              <el-table :data="goodlist" style="width: 100%" size="medium">
                <el-table-column width="200" prop="goodName" label="商品名称"></el-table-column>
                <el-table-column width="120" prop="number" label="数量"></el-table-column>
                <el-table-column width="120" prop="type" label="支付类型">
                  <template slot-scope="scope">
                    {{ geType(scope.row.type) }}
                  </template>
                </el-table-column>
                <el-table-column width="140" prop="price" label="支付单价/元">
                  <template slot-scope="scope">
                    {{getPrice(scope.row.type,0,scope.row.price)}}
                  </template>
                </el-table-column>
                <el-table-column width="140" prop="vipScore" label="支付积分">
                  <template slot-scope="scope">
                    {{getPrice(scope.row.type,1,scope.row.vipScore)}}
                  </template>
                </el-table-column>
              </el-table>
              <div slot="footer" class="dialog-footer">
                <el-button @click="closeShowMessage">返 回</el-button>
              </div>
            </el-dialog>



          </el-col>
        </el-row>
      </el-form>
    </div>

    <br>


    <!--表格数据-->
    <div class="table-container">
      <span style="margin-right: 91%;padding: 10px;"><b>出库信息</b></span>
      <el-divider class="custom-divider"></el-divider>
      <el-table :data="maindata" style="width: 100%" size="medium">
        <el-table-column prop="purchaseId" width="150" label="购买订单编号"> </el-table-column>
        <el-table-column width="200" prop="userName" label="用户名"></el-table-column>
        <el-table-column width="150" prop="paid" label="支付金额/元"></el-table-column>
        <el-table-column width="150" prop="score" label="支付积分"></el-table-column>
        <el-table-column width="200" prop="orderTime" label="购买时间">
          <template slot-scope="scope">
            {{ decoder(scope.row.orderTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="320" fixed="right">
          <template v-slot="scope">
            <el-button style="font-size: 16px" type="primary" @click="openShowMessage(scope.row)"> 订单详情</el-button>
            <el-button style="font-size: 16px" type="danger" @click="deleteOpen(scope.row)"> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage4"
        background layout="total, prev, pager, next, jumper" :total="400">
      </el-pagination>
    </div>
  </div>
</template>
  
<script>
import axios from 'axios';
export default {
  data() {
    return {
      empId: "",
      cusId: "",
      username: "",
      password: "",
      purchaseId: "",
      maindata: [
        {
          purchaseId: "1",
          userName: "Frevoid",
          paid: "90",
          score: "10",
          orderTime: "2023-12-31T12:32:00.210Z"
        },
        {
          purchaseId: "2",
          userName: "huangkang",
          paid: "580",
          score: "580",
          orderTime: "2024-01-01T09:49:51.000Z"
        },
      ],
      goodlist: [
        {
          goodName: '农夫山泉',
          number: '1',
          type: "0",
          price: '2',
          vipScore: '0',
        },
        {
          goodName: '保温杯',
          number: '1',
          type: "1",
          price: '0',
          vipScore: '20',
        },
        {
          goodName: '雨伞',
          number: '1',
          type: "0",
          price: '30',
          vipScore: '0',
        }
      ],
      inputMessage: false,
      showMessage: false,
      selform: {
        purchaseId: "",
        userName: "",
        orderTime: "",
      },
      deleteform: {
        purchaseId: "",
        userName: "",
        paid: "",
        score: "",
        orderTime: "",
      },
      formLabelWidth: '80px',
      formLabelWidthTime: '85px'
    };
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },

    getMaindata() {
      const url = "http://localhost:8080/outstore";
      axios.get(url)
        .then((response) => {
          console.log(response.data);
          this.maindata = response.data.data.rows;
        })
        .catch((error) => {
          console.error('Error in getMaindata request', error);
        });
    },
    init() {
      this.empId = localStorage.getItem('empId');
      this.cusId = localStorage.getItem('cusId');
      this.username = localStorage.getItem('username') || "";
      this.password = localStorage.getItem('password') || "";
    },
    getPrice(type,flag,price){
      if(type==flag)
        return price;
      else  
        return "--"
    },
    selDate() {
      const url = "http://localhost:8080/outstore";
      axios.get(url, {
        params: this.selform
      }).then((response) => {
        console.log(response.data);
        this.maindata = response.data.data.rows;
      })
        .catch((error) => {
          console.error('Error in selData request', error);
        });
    },

    delData() {
      let url = "http://localhost:8080/outstore/";
      url += this.deleteform.purchaseId;
      axios.delete(url).then((response) => {
        console.log(response.data);
        this.selDate();
      })
        .catch((error) => {
          console.error('Error in delData request', error);
        });
    },

    inputData() {
      const url = "http://localhost:8080/outstore";
      axios.post(url, this.inputform
      ).then((response) => {
        console.log(response.data);
        this.selDate();
      })
        .catch((error) => {
          console.error('Error in inputData request', error);
        });
    },

    updateData() {
      const url = "http://localhost:8080/outstore";
      axios.put(url, this.updateform
      ).then((response) => {
        console.log(response.data);
        this.selDate();
      })
        .catch((error) => {
          console.error('Error in updateData request', error);
        });
    },

    showdetailData() {
      let url = "http://localhost:8080/outstoredetail";
      const dataList={
        purchaseId:this.purchaseId,
      };
      axios.get(url, { params: dataList })
        .then((response) => {
          console.log(response.data);
          this.goodlist = response.data.data.rows;
        })
        .catch((error) => {
          console.error('Error in showdetailData request', error);
        });
    },

    geType(type) {
      const TypeMap = {
        0: "金钱支付",
        1: "积分支付",
      };
      return TypeMap[type];
    },

    decoder(time) {
      let arr = time.split("T");
      let str1 = arr[0];
      let str2 = arr[1].split("Z")[0].split(".")[0];
      let result = `${str1} ${str2}`;
      return result;
    },

    submitselMessage() {
      this.selDate();
    },
    checkid() {
      if (this.empId == "" || this.empId == null)
        this.$router.push('/login');
    },

    deleteOpen(row) {
      this.deleteform.purchaseId = row.purchaseId;
      this.deleteform.userName = row.userName;
      this.deleteform.score = row.score;
      this.deleteform.paid = row.paid;
      this.deleteform.orderTime = row.orderTime;
      this.$confirm("此操作将删除该条订单信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.delData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.submitselMessage();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    openShowMessage(row) {
      this.purchaseId = row.purchaseId;
      //获取对应订单数据
      this.showdetailData();
      this.showMessage = true;
    },
    closeShowMessage() {
      this.showMessage = false;
    },

    pickerOptions: {
      shortcuts: [{
        text: '今天',
        onClick(picker) {
          picker.$emit('pick', new Date());
        }
      }, {
        text: '昨天',
        onClick(picker) {
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24);
          picker.$emit('pick', date);
        }
      }, {
        text: '一周前',
        onClick(picker) {
          const date = new Date();
          date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
          picker.$emit('pick', date);
        }
      }]
    },
  },
  mounted() {
    this.init();
    this.checkid();
    this.getMaindata();
  }
};
</script>
  
<style>
.router-link-active {
  text-decoration: none;
}

.table-container {
  box-shadow: 0 0 4px 1px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .12);
}

.custom-divider {
  margin: 4px 0;
  /* 调整 el-divider 的间距 */
}

.custom-descriptions {
  padding-left: 10px;
  margin-bottom: 0;
}

.all {
  line-height: 30px;
}
</style>